<template>
  <div>
    <el-dialog
      :title="userForm.show ? '编辑' : '新增'"
      :visible.sync="is.show"
      :before-close="cancel"
    >
      <el-form :model="userForm" :rules="rules" ref="userForm">
        <el-form-item
          label="所属角色"
          :label-width="formLabelWidth"
          prop="roleid"
        >
          <el-select v-model="userForm.roleid" placeholder="请选择活动区域">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="用户名称"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="userForm.username"
            clearable
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            v-model="userForm.password"
            type="password"
            show-password
            clearable
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="userForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入管理员添加或者编辑接口
import { addOreditUser } from "../../request/api";
export default {
  data() {
    return {
      //表单数据
      userForm: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      formLabelWidth: "120px",
      //表单验证规则
      rules: {
        roleid: [
          { required: true, message: "请选择所属角色", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 2, max: 10, message: "请输入2-6位字符", trigger: "blur" },
        ],
      },
    };
  },

  //计算属性  用来使用辅助性函数mapState
  computed: {
    //这是管理员管理的数据
    ...mapState("user", ["userList", "is", "page", "size", "total"]),

    //这是角色管理的数据 为了选择所属角色
    ...mapState("role", ["roleList"]),
  },

  //方法 用来使用 辅助性函数 mapActions
  methods: {
    ...mapActions("user", ["getUser", "getTotal"]),

    //获取所属角色的方法
    ...mapActions("role", ["getRole"]),

    //点击取消  重置表单事件
    cancel() {
      //关闭弹窗
      this.$store.commit("user/changeDialog", false);
      //清空表单
      this.userForm = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
      //清空表单验证
      this.$refs.userForm.resetFields();
    },

    //点击确认事件
    enter() {
      let url = this.userForm.show ? "useredit" : "useradd";
      this.$refs.userForm.validate((res) => {
        if (res) {
          addOreditUser(url, this.userForm).then((res) => {
            if (res.code == 200) {
              //关闭弹窗
              this.cancel();
              //重新获取总数
              this.getTotal();

              //判断是添加还是编辑
              if (res.msg == "操作成功") {
                //重新获取数据  这是编辑
                this.getUser({
                  page: this.page,
                  size: this.size,
                });
              } else {
                //重新获取数据  这是添加
                this.getUser({
                  page: Math.ceil((this.total + 1) / this.size),
                  size: this.size,
                });
                //改变当前页码数
                this.$store.commit("user/changeSelect", true);
              }
              //提示添加 或 编辑成功
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },

    //调用父组件里面的改变change
    changeParPage() {
      // console.log(123);
      this.$emit("changeParPage");
    },

    //用来传输数据的方法 由父组件调用
    dataForm(obj) {
      this.userForm = obj;
    },
  },

  //挂载完成 获取角色数据
  mounted() {
    if (this.$store.state.role.roleList.length == 0) {
      this.getRole();
    }
  },
};
</script>


<style scoped>
</style>